package com.web.expspringboot.mapper;

import com.web.expspringboot.dto.Contact;
import com.web.expspringboot.entity.ProductSt;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface PurchaseMapper {

    @Insert("INSERT INTO purchase_st (product_id, owner_id, customer_id, order_time) VALUES (#{productId}, #{ownerId}, #{customerId}, CURRENT_TIMESTAMP)")
    boolean addNewPurchase(long productId, long ownerId, long customerId);

    @Select("SELECT " +
            "p.product_id AS productId, " +
            "p.product_title AS productTitle, " +
            "p.product_intro AS productIntro, " +
            "p.product_price AS productPrice, " +
            "p.product_imgs AS productImgs, " +
            "c.order_time AS publishTime, " +
            "p.owner_id AS ownerId, " +
            "p.location AS location, " +
            "p.status AS status " +
            "FROM product_st p " +
            "JOIN purchase_st c ON p.product_id = c.product_id " +
            "WHERE c.customer_id = #{userId}"
            + " ORDER BY c.order_time DESC ")
    List<ProductSt> getPurchaseProducts(long userId);

    @Delete("DELETE FROM purchase_st WHERE product_id = #{productId}")
    boolean deletePurchase(long productId);

    @Select("SELECT o.user_account AS ownerPhone, c.user_account AS customerPhone " +
            "FROM user_st o " +
            "JOIN purchase_st p ON o.user_id = p.owner_id " +
            "JOIN user_st c ON c.user_id = p.customer_id " +
            "WHERE p.product_id = #{productId}")
    Contact getContact(long productId);

}
